package com.glodon.async.threadpool;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: lilin
 * @Date: 2022/8/14
 */
public class DynamicThreadPoolTest {

    public static void main(String[] args) throws Exception {
        String poolName = "测试动态线程池";

        // 创建一个业务名为：poolName的线程池
        // 可根据不同的业务创建多个不同的线程池
        DynamicThreadPoolExecutor threadPoolExecutor = DynamicThreadPoolManager.createDynamicThreadPoolExecutor(poolName, 10, 20, 0L, TimeUnit.MILLISECONDS, new ResizableCapacityLindedBlockingQueue<>(10));

        // 执行若干任务
        for (int i = 0; i < 30; i++) {
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(200L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "=============动态改变前...");
                }
            },  "taskBefore");
        }

        DynamicThreadPoolManager.printThreadPoolExecutor(poolName);
        DynamicThreadPoolManager.printTaskStatus(poolName);

        Thread.sleep(1000L);
        // 动态修改线程池参数
        DynamicThreadPoolManager.refreshThreadPoolExecutor(poolName, 20, 60, threadPoolExecutor.getKeepAliveTime(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS, 30);

        for (int i = 0; i < 60; i++) {
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(300L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "=============动态修改后...");
                }
            },  "taskAfter");
        }

        Thread.sleep(2000L);

        DynamicThreadPoolManager.printThreadPoolExecutor(poolName);
        DynamicThreadPoolManager.printTaskStatus(poolName);

        threadPoolExecutor.shutdown();

    }

}
